﻿using System.Web.Hosting;

namespace OpenWaves.ImageTransformations.Web
{
    public class VirtualPathFileProvider : IVirtualFileProvider
    {
        private readonly VirtualPathProvider innerProvider;
        protected VirtualPathProvider InnerProvider
        {
            get { return this.innerProvider; }
        }

        public VirtualPathFileProvider(VirtualPathProvider innerProvider)
        {
            this.innerProvider = innerProvider;
        }

        public virtual IVirtualFile GetFile(Url fileUrl)
        {
            var url = fileUrl as RelativeUrl;
            if (url == null)
                return null;

            var file = this.InnerProvider.GetFile(url.Path.ToUnescapedString());
            if (file == null)
                return null;

            return this.CreateVirtualFileWrapper(file, url);
        }

        protected virtual VirtualFileWrapper CreateVirtualFileWrapper(VirtualFile file, RelativeUrl url)
        {
            return new VirtualFileWrapper(url, file, this.InnerProvider);
        }
    }
}